﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;


namespace MiniBug.Authentication
{
	/// <summary>
	/// AspNetMembershipProvider implementation to support authentication
	/// </summary>
	public class MiniBugMembershipProvider:MembershipProvider
	{

		public override string ApplicationName
		{
			get
			{
				return "MiniBug";
			}
			set {
				throw new NotImplementedException();
			}
			
		}

		public override bool ChangePassword(string username, string oldPassword, string newPassword)
		{
			throw new NotImplementedException();
		}

		public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="username"></param>
		/// <param name="password"></param>
		/// <param name="email"></param>
		/// <param name="passwordQuestion"></param>
		/// <param name="passwordAnswer"></param>
		/// <param name="isApproved"></param>
		/// <param name="providerUserKey"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, 
													string passwordAnswer, bool isApproved, object providerUserKey, 
													out MembershipCreateStatus status)
		{

			return this.CreateUser(null, username, password, email, false, false, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
		}


		public MembershipUser CreateUser(string name, string username, string password, string email, bool isAdmin, bool isVirtual, string passwordQuestion,
													string passwordAnswer, bool isApproved, object providerUserKey,
													out MembershipCreateStatus status)
		{
			status = new MembershipCreateStatus();
			MembershipUser newUser = null;

			try
			{
				using (Entity.MSSQLEntity dbContext = new Entity.MSSQLEntity())
				{
					Entity.User user = new Entity.User();
					user.Name = name;
					user.Login = username;
					user.Password = CryptographProvider.Encrypt(password,username);
					user.Email = email;
					user.isVirtual = isVirtual;
					user.isAdmin = isAdmin;
					dbContext.AddToUser(user);
					dbContext.SaveChanges();
				}
				status = MembershipCreateStatus.Success;
				newUser = new MembershipUser("MiniBugMembershipProvider", username, password, email, passwordQuestion, passwordAnswer, isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
			}
			catch (Exception)
			{

				throw;
			}
			return newUser;
		}
		public override bool DeleteUser(string username, bool deleteAllRelatedData)
		{
			throw new NotImplementedException();
		}

		public override bool EnablePasswordReset
		{
			get { throw new NotImplementedException(); }
		}

		public override bool EnablePasswordRetrieval
		{
			get { throw new NotImplementedException(); }
		}

		public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
		{
			throw new NotImplementedException();
		}

		public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
		{
			throw new NotImplementedException();
		}

		public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
		{
			throw new NotImplementedException();
		}

		public override int GetNumberOfUsersOnline()
		{
			throw new NotImplementedException();
		}

		public override string GetPassword(string username, string answer)
		{
			throw new NotImplementedException();
		}

		public override MembershipUser GetUser(string username, bool userIsOnline)
		{
			throw new NotImplementedException();
		}

		public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
		{
			throw new NotImplementedException();
		}

		public override string GetUserNameByEmail(string email)
		{
			throw new NotImplementedException();
		}

		public override int MaxInvalidPasswordAttempts
		{
			get { throw new NotImplementedException(); }
		}

		public override int MinRequiredNonAlphanumericCharacters
		{
			get { throw new NotImplementedException(); }
		}

		public override int MinRequiredPasswordLength
		{
			get { return 6; }
		}

		public override int PasswordAttemptWindow
		{
			get { throw new NotImplementedException(); }
		}

		public override MembershipPasswordFormat PasswordFormat
		{
			get { throw new NotImplementedException(); }
		}

		public override string PasswordStrengthRegularExpression
		{
			get { throw new NotImplementedException(); }
		}

		public override bool RequiresQuestionAndAnswer
		{
			get { throw new NotImplementedException(); }
		}

		public override bool RequiresUniqueEmail
		{
			get { throw new NotImplementedException(); }
		}

		public override string ResetPassword(string username, string answer)
		{
			throw new NotImplementedException();
		}

		public override bool UnlockUser(string userName)
		{
			throw new NotImplementedException();
		}

		public override void UpdateUser(MembershipUser user)
		{
			using (Entity.MSSQLEntity dbContext = new Entity.MSSQLEntity())
			{
				Entity.User dbUser = dbContext.User
					.Where(u => u.Login.Equals(user.UserName) && u.Password.Equals(user.GetPassword()))
					.FirstOrDefault();
				if (user == null)
				{
				  
				}
				else
				{
						
				}   
			}     
		}

		public override bool ValidateUser(string username, string password)
		{
			password = CryptographProvider.Encrypt(password,username);

		   using( Entity.MSSQLEntity dbContext = new Entity.MSSQLEntity())
				{
					  Entity.User user = dbContext.User
						  .Where(u => u.Login.Equals(username) && u.Password.Equals(password))
						  .FirstOrDefault();
					  if (user == null) 
						  return false;
					  else
						  return true;
				}         
		}
	}
}